#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
创建代码整理工具的简约风格ICO图标
"""

from PIL import Image, ImageDraw, ImageFont
import os

def create_code_organizer_icon():
    """创建代码整理工具的简约图标"""
    
    # 创建256x256的基础图像，使用透明背景
    img = Image.new('RGBA', (256, 256), (0, 0, 0, 0))
    draw = ImageDraw.Draw(img)
    
    # 定义颜色方案 - 简约蓝色渐变
    primary_color = (52, 152, 219)    # 主蓝色
    secondary_color = (41, 128, 185)  # 深蓝色
    accent_color = (236, 240, 241)    # 浅灰色
    
    # 绘制背景圆角矩形
    margin = 20
    draw.rounded_rectangle(
        [margin, margin, 256-margin, 256-margin], 
        radius=30, 
        fill=primary_color
    )
    
    # 绘制代码块图案 - 简约线条表示
    line_height = 16
    start_y = 80
    
    # 绘制代码行
    for i in range(8):
        y_pos = start_y + i * line_height
        if i % 3 == 0:  # 缩进行
            draw.line([70, y_pos, 180, y_pos], fill=accent_color, width=6)
        elif i % 3 == 1:  # 普通行
            draw.line([60, y_pos, 200, y_pos], fill=accent_color, width=6)
        else:  # 短行
            draw.line([60, y_pos, 160, y_pos], fill=accent_color, width=6)
    
    # 绘制文件夹图标轮廓（简约表示）
    folder_color = secondary_color
    # 文件夹顶部
    draw.rectangle([80, 160, 180, 200], fill=folder_color)
    # 文件夹底部
    draw.rectangle([70, 180, 190, 220], fill=folder_color)
    
    # 添加整理箭头符号
    arrow_color = (255, 255, 255)
    # 向上箭头
    arrow_x, arrow_y = 200, 120
    draw.line([arrow_x, arrow_y+20, arrow_x, arrow_y], fill=arrow_color, width=4)
    draw.line([arrow_x-8, arrow_y+8, arrow_x, arrow_y], fill=arrow_color, width=4)
    draw.line([arrow_x+8, arrow_y+8, arrow_x, arrow_y], fill=arrow_color, width=4)
    
    return img

def create_icon_sizes(source_img):
    """创建不同尺寸的图标"""
    sizes = [(16, 16), (32, 32), (48, 48), (64, 64), (128, 128), (256, 256)]
    icons = []
    
    for size in sizes:
        resized = source_img.resize(size, Image.Resampling.LANCZOS)
        icons.append(resized)
    
    return icons

def save_ico_file():
    """保存ICO文件"""
    try:
        # 创建主图标
        main_icon = create_code_organizer_icon()
        
        # 创建不同尺寸的图标
        icons = create_icon_sizes(main_icon)
        
        # 保存ICO文件
        ico_path = "code_organizer.ico"
        icons[0].save(
            ico_path,
            format='ICO',
            sizes=[(16, 16), (32, 32), (48, 48), (64, 64), (128, 128), (256, 256)],
            append_images=icons[1:]
        )
        
        print(f"图标已成功保存为: {ico_path}")
        print(f"图标包含尺寸: 16x16, 32x32, 48x48, 64x64, 128x128, 256x256")
        
        # 同时保存为PNG格式供预览
        png_path = "code_organizer_icon.png"
        main_icon.save(png_path, format='PNG')
        print(f"预览图已保存为: {png_path}")
        
        return True
        
    except ImportError as e:
        print(f"错误: 需要安装Pillow库。请运行: pip install Pillow")
        return False
    except Exception as e:
        print(f"保存图标时出错: {e}")
        return False

if __name__ == "__main__":
    save_ico_file()